package com.spa.domain.service;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.spa.application.command.admin.ShopWithdrawPayCommand;
import com.spa.domain.entity.ShopWithdrawRecord;
import com.spa.infrastructure.enums.withdraw.WithdrawStatusEnum;
import com.spa.infrastructure.mapper.ShopWithdrawRecordMapperStruct;
import com.spa.infrastructure.repository.condition.ShopWithdrawRecordCondition;
import com.spa.infrastructure.repository.po.ShopWithdrawRecordPO;
import com.spa.infrastructure.repository.service.ShopWithdrawRecordRepositoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class ShopWithdrawRecordService {

    @Autowired
    private ShopWithdrawRecordRepositoryService shopWithdrawRecordRepositoryService;

    public void add(ShopWithdrawRecordPO po) {
        po.setStatus(WithdrawStatusEnum.$10.getCode());
        po.setApplyTime(new Date());
        shopWithdrawRecordRepositoryService.add(po);
    }

    public Page<ShopWithdrawRecord> page(ShopWithdrawRecordCondition condition) {
        return ShopWithdrawRecordMapperStruct.INSTANCE.po2Entity4Page(shopWithdrawRecordRepositoryService.page(condition));
    }

    public List<ShopWithdrawRecord> list(ShopWithdrawRecordCondition condition) {
        return ShopWithdrawRecordMapperStruct.INSTANCE.po2Entity4List(shopWithdrawRecordRepositoryService.list(condition));
    }

    public ShopWithdrawRecord getDetailById(long id){
        return ShopWithdrawRecordMapperStruct.INSTANCE.po2Entity(shopWithdrawRecordRepositoryService.getShopWithdrawRecordById(id));
    }

    public void adminAudit(long id, long auditId, String auditName) {
        ShopWithdrawRecordPO po = new ShopWithdrawRecordPO();
        po.setId(id);
        po.setAuditTime(new Date());
        po.setStatus(WithdrawStatusEnum.$20.getCode());
        po.setAuditId(auditId);
        po.setAuditName(auditName);
        shopWithdrawRecordRepositoryService.updateShopWithdrawRecord(po);
    }

    public void payForShop(ShopWithdrawPayCommand command) {
        ShopWithdrawRecordPO po = new ShopWithdrawRecordPO();
        po.setId(command.getWithdrawId());
        po.setCredentials(command.getCredentials());
        po.setStatus(WithdrawStatusEnum.$30.getCode());
        po.setPayTime(new Date());
        shopWithdrawRecordRepositoryService.updateShopWithdrawRecord(po);
    }
}
